package androidx.lifecycle

/**
 * @author : Shonn.Zhang
 * @email  : shonn.zhang@mart.blue
 * @time   : 2021/6/25 11:49
 * @desc   : 扩展Observer功能，解决LiveData粘性问题
 */
open class ExtendObserver<T>(private val liveData: LiveData<T>,val observer: Observer<in T>) : Observer<T> {

    private var lastVersion = liveData.version

    override fun onChanged(t: T) {
        //拦截注册之前的状态发送
        if (lastVersion >= liveData.version) {
            return
        }

        lastVersion = liveData.version
        observer.onChanged(t)
    }

}